package net.tngou.iask.action.my;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.math.NumberUtils;

import net.tngou.iask.action.BaseAction;
import net.tngou.iask.jdbc.OrderType;
import net.tngou.iask.jdbc.PageUtil;
import net.tngou.iask.pojo.Access;
import net.tngou.iask.pojo.Accessresult;
import net.tngou.iask.pojo.Cure;
import net.tngou.iask.pojo.Disease;
import net.tngou.iask.pojo.Health;
import net.tngou.iask.pojo.Linkman;
import net.tngou.iask.pojo.POJO;
import net.tngou.iask.pojo.Patient;
import net.tngou.iask.pojo.Patient_main;
import net.tngou.iask.pojo.Patient_user;
import net.tngou.iask.pojo.Survey;
import net.tngou.iask.service.AccessService;
import net.tngou.iask.service.CureService;
import net.tngou.iask.service.DiseaseService;
import net.tngou.iask.service.HealthService;
import net.tngou.iask.service.MemoService;
import net.tngou.iask.service.PatientService;
import net.tngou.iask.service.SurveyService;

/**
 * 患者
 * @author tngou@tngou.net
 *
 */
public class PatientAction extends BaseAction {

	/**
	 * 患者列表
	 */
	public void list() {
		PatientService service = new PatientService();
		PageUtil page = service.getPage(ask.getPage(), ask.getSize(), user.getMain(), ask.getKeyword());
		root.put("page", page);
		printFreemarker("my/patient_list.ftl", root);
	}
	
	/**
	 * 添加患者
	 */
	public void add() {
		printFreemarker("my/patient_add.ftl", root);
	}
	
	/**
	 * 更新患者
	 */
	public void update() {
		
	}
	
	/**
	 * 患者联系人
	 */
	@SuppressWarnings({ "unchecked", "serial" })
	public void linkman() {
		long id=ask.getId();
		if(id==0){run_404();return;}
		Patient patient = new Patient().get(id);
		if(patient==null){run_404();return;}
		List<Linkman> list = new ArrayList<>();
		//权限控制
		if(user.getRole()==1||user.getRole()==2)
		{			
			list=(List<Linkman>) new Linkman().list(new HashMap<String,Object>(){{put("patient", patient.getId());}},"relation",OrderType.ASC);
		}else{//管理者
			if(user.getMain()!=0)
			{
				POJO pojo = new Patient_main().get("patient="+patient.getId()+" AND main="+user.getMain());
				if(pojo!=null)
				{
					list=(List<Linkman>) new Linkman().list(new HashMap<String,Object>(){{put("patient", patient.getId());}},"relation",OrderType.ASC);
				}
			}	
		}
		root.put("patient", patient);
		root.put("list", list);
		printFreemarker("my/patient_linkman.ftl", root);
		
	}
	
	/**
	 * 添加联系人
	 */
	public void linkmanadd() {
		long id=ask.getId();
		if(id==0){run_404();return;}
		Patient  patient= new Patient().get(id);
		if(patient==null){run_404();return;}
		root.put("patient", patient);
		printFreemarker("my/patient_linkmanadd.ftl", root);
	}
	
	/**
	 * 更新联系人
	 */
	public void linkmanupdate() {
		long id=ask.getId();
		if(id==0){run_404();return;}
		Linkman linkman = new Linkman().get(id);
		if(linkman==null){run_404();return;}
		Patient  patient= new Patient().get(linkman.getPatient());
		if(patient==null){run_404();return;}
		root.put("patient", patient);
		root.put("linkman", linkman);
		printFreemarker("my/patient_linkmanupdate.ftl", root);
	}
	
	/**
	 * 个人健康档案
	 */
	public void health() {
		long id=ask.getId();
		if(id==0){run_404();return;}
		Patient patient = new Patient().get(id);
		if(patient==null){run_404();return;}
		HealthService service = new HealthService();
		AccessService accessService = new AccessService();
		List<?> list = accessService.getList(user.getMain(), patient.getId(), 1);
		Map<String, List<Health>> maps = service.getList(patient.getId());
		root.put("patient", patient);
		root.put("list", list);
		root.put("maps", maps);
		printFreemarker("my/patient_health.ftl", root);
	}
	
	/**
	 * 治疗列表
	 */
	public void cure() {
		long id=ask.getId();
		if(id==0){run_404();return;}
		Patient patient = new Patient().get(id);
		if(patient==null){run_404();return;}
		CureService service = new CureService();
		List<?> list = service.getList(user.getMain(), patient.getId());
		root.put("patient", patient);
		root.put("list", list);
		printFreemarker("my/patient_cure.ftl", root);
	}
	/**
	 * 治疗添加
	 */
	public void cureadd() {
		long id=ask.getId();
		if(id==0){run_404();return;}
		Patient patient = new Patient().get(id);
		if(patient==null){run_404();return;}
		root.put("patient", patient);
		DiseaseService service = new DiseaseService();
		List<Map<String, Object>> list = service.getList(user.getMain());
		root.put("list", list);
		printFreemarker("my/patient_cureadd.ftl", root);
	}
	
	/**
	 * 随访记录
	 */
	public void access() {
		long id=ask.getId();
		if(id==0){run_404();return;}
		Patient patient = new Patient().get(id);
		if(patient==null){run_404();return;}
		AccessService service = new AccessService();
		List<?> list = service.getList(user.getMain(), patient.getId(), 2);
		root.put("patient", patient);
		root.put("list", list);
		printFreemarker("my/patient_access.ftl", root);
	}
	
	/**
	 * 添加随访
	 */
	public void accessadd() {
		long id=ask.getId();
		if(id==0){run_404();return;}
		int type=0;//访问类型 0 普通 1健康 2 随访
		String[] params = request.getParams();
		if(params!=null&&params.length>1&&NumberUtils.isNumber(params[1]))
		{	
			type=Integer.parseInt(params[1]);			
		}
		Patient patient = new Patient().get(id);
		if(patient==null){run_404();return;}
		List<?> list=null;
		SurveyService service = new SurveyService();
		switch (type) {
		case 0:		
			list = service.getList(user.getMain(), type);
			break;
		case 1:
			list = service.getList(user.getMain(), type);
			break;
		case 2:
			DiseaseService diseaseService = new DiseaseService();
			list=diseaseService.getList(user.getMain(),patient.getId());
			break;
		default:
			break;
		}
		root.put("list",list);
		root.put("type",type);
		root.put("patient", patient);
		printFreemarker("my/patient_accessadd.ftl", root);
	}
	
	/**
	 * 附件
	 */
	public void annex() {
		long id=ask.getId();
		if(id==0){run_404();return;}
		Patient patient = new Patient().get(id);
		if(patient==null){run_404();return;}
		root.put("patient", patient);	
		printFreemarker("my/patient_annex.ftl", root);
	}
	/**
	 * 添加附件
	 */
	public void annexadd() {
		long id=ask.getId();
		if(id==0){run_404();return;}
		Patient patient = new Patient().get(id);
		if(patient==null){run_404();return;}
		root.put("patient", patient);
		printFreemarker("my/patient_annexadd.ftl", root);
	}
	
	
	/**
	 * 病人消息
	 */
	@SuppressWarnings("unchecked")
	public void show() {
		long id=ask.getId();
		if(id==0){run_404();return;}
		Patient patient = new Patient().get(id);
		if(patient==null){run_404();return;}
		
		
		HealthService service = new HealthService();
		AccessService accessService = new AccessService();
		List<?> heaths = accessService.getList(user.getMain(), patient.getId(), 1);
		Map<String, List<Health>> heathmaps = service.getList(patient.getId());
		root.put("heaths", heaths);
		root.put("heathmaps", heathmaps);
		
		List<?> accesses = accessService.getList(user.getMain(), patient.getId(), 2);
		root.put("accesses", accesses);
		CureService cureservice = new CureService();
		List<?> cures = cureservice.getList(user.getMain(), patient.getId());
		root.put("cures", cures);
		List<Linkman> linkmains = (List<Linkman>) new Linkman().list(new HashMap<String,Object>(){{put("patient", patient.getId());}},"relation",OrderType.ASC);
		root.put("linkmains", linkmains);
		
		MemoService memoService = new MemoService();
		List<Map<String, Object>> memos = memoService.getPatient(patient.getId());
		root.put("memos", memos);
		
		root.put("patient", patient);
		printFreemarker("my/patient_show.ftl", root);
	}
	
	
}
